﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BZLib;
using BZLib.Global;
using BZLib.Network;

namespace CSBZAdmin
{
    public partial class Main : Form
    {
        private ServerConnection sLink;
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            sLink = new ServerConnection();
            WriteText("Connecting to localhost on port 5154");

            try
            {
                sLink.Connect("localhost", 5154);
            }
            catch
            {
                WriteText("Unable to connect to server.");
                return;
            }

            WriteText("Connected.");
            sLink.Callbacks += Callback;
            EnterGame();
        }

        private void EnterGame()
        {
            EnterDataSent data = new EnterDataSent();
            data.Callsign = "CSBZAdmin";
            data.Email = "CSBZAdmin";
            data.Team = TeamName.Automatic;
            sLink.Send(data);
        }

        private void WriteText(string text, Color color)
        {
            text += "\n";
            int startIndex = OutputText.Text.Length;
            OutputText.AppendText(text);
            OutputText.Select(startIndex, text.Length);
            OutputText.SelectionColor = color;
            OutputText.Select(0, 0);
        }
        private void WriteText(string text)
        {
            WriteText(text, Color.Black);
        }

        private void Callback(NetMessage msg, IMessageDataReceived eventData)
        {
            switch (msg)
            {
                case NetMessage.Accept:
                    WriteText("Sucessfully joined server.", Color.Red);
                    break;

                case NetMessage.ChatMessage:
                    ChatMessageDataReceived data = (ChatMessageDataReceived)eventData;
                    WriteText(String.Format("{0}: {1}", data.From, data.Text));
                    break;
                
                case NetMessage.Reject:
                    WriteText("Server rejected us :(", Color.Red);
                    break;
            }
        }

        private void SendChat()
        {
            string text = ChatBar.Text;
            ChatBar.Text = ""; // Clear text

            ChatMessageDataSent data = new ChatMessageDataSent();
            data.Text = text;
            data.Recipient = (int)NetPlayer.All;
            sLink.Send(data);
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            sLink.Disconnect();
        }

        private void SendButton_Click(object sender, EventArgs e)
        {
            SendChat();
        }

        private void ChatBar_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13) // Enter key
            {
                SendChat();
            }
        }
    }
}
